package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.constant.DownloadTaskStatusEnum;
import com.sikaryofficial.backend.domain.entity.DownloadTask;
import com.sikaryofficial.backend.mapper.DownloadTaskMapper;
import com.sikaryofficial.backend.service.IDownloadTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 下载任务Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-01-12
 */
@Service
@SuppressWarnings("all")
public class DownloadTaskServiceImpl extends ServiceImpl<DownloadTaskMapper, DownloadTask> implements IDownloadTaskService {
    @Autowired
    private DownloadTaskMapper downloadTaskMapper;

    /**
     * 查询最新下载任务
     *
     * @param objectId   对象ID
     * @param taskType   任务类型
     * @param taskStatus 任务状态
     * @return
     */
    @Override
    public List<DownloadTask> getLatestTaskListById(Long objectId, Integer taskType) {
        return this.lambdaQuery().eq(DownloadTask::getDeletedVersion, 0L)
                .eq(DownloadTask::getTaskStatus, DownloadTaskStatusEnum.DOWNLOADABLE.getCode())
                .eq(DownloadTask::getTaskType, taskType)
                .eq(DownloadTask::getObjectId, objectId)
                .orderByDesc(DownloadTask::getCreatedTime, DownloadTask::getTaskId)
                .last(" limit 5 ")
                .list();
    }

    /**
     * 查询下载任务
     *
     * @param taskType 任务类型
     * @return
     */
    @Override
    public List<DownloadTask> getLatestTaskList(Integer taskType) {
        return this.lambdaQuery().eq(DownloadTask::getDeletedVersion, 0L)
                .eq(DownloadTask::getTaskStatus, DownloadTaskStatusEnum.DOWNLOADABLE.getCode())
                .eq(DownloadTask::getTaskType, taskType)
                .orderByDesc(DownloadTask::getCreatedTime, DownloadTask::getTaskId)
                .last(" limit 5 ")
                .list();

    }

    @Override
    public void initDownloadTask(Long userId, Long objectId, Long taskId, Integer taskType) {
        this.save(DownloadTask.builder().createdTime(new Date())
                .createdBy(userId).taskId(taskId)
                .taskStatus(DownloadTaskStatusEnum.GENERATING.getCode())
                .taskType(taskType)
                .workLoad(1L).objectId(objectId).build());
    }

    @Override
    public void initDownloadWithRemark(Long userId, Long objectId, Long taskId, Integer taskType, String remark) {
        this.save(DownloadTask.builder().createdTime(new Date())
                .createdBy(userId).taskId(taskId)
                .taskStatus(DownloadTaskStatusEnum.GENERATING.getCode())
                .taskType(taskType)
                .remark(remark)
                .workLoad(1L).objectId(objectId).build());
    }

    @Override
    public void updateDownloadTask(Long userId, Long taskId, String offlineUrl) {
        this.update(DownloadTask.builder()
                        .taskStatus(DownloadTaskStatusEnum.DOWNLOADABLE.getCode())
                        .updatedBy(userId).updatedTime(new Date()).offlineUrl(offlineUrl).build(),
                new LambdaUpdateWrapper<DownloadTask>().eq(DownloadTask::getTaskId, taskId)
                        .eq(DownloadTask::getTaskStatus, DownloadTaskStatusEnum.GENERATING.getCode()));

    }
}
